//
//  UITableViewCell+LXSaparateLine.m
//  ProjectSource_Demo
//
//  Created by Liu on 15/11/26.
//  Copyright © 2015年 AngryBear. All rights reserved.
//

#import "UITableViewCell+LXSaparateLine.h"
#import <objc/runtime.h>

const static NSString *kLXTopLineKEY = @"kLXTopLineKEY";
const static NSString *kLXBottomLineKEY = @"kLXBottomLineKEY";

@implementation UITableViewCell (LXSaparateLine)

- (UIImageView *)lx_topLine
{
    UIImageView *line = objc_getAssociatedObject(self, &kLXTopLineKEY);
    if (!line.superview) {
        line = [[UIImageView alloc] init];
        [self setLx_topLine:line];
        [self.contentView addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        line.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[line]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(line)]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[line(0.5)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(line)]];
    }
    return line;
}

- (void)setLx_topLine:(UIImageView *)lx_topLine
{
    objc_setAssociatedObject(self, &kLXTopLineKEY, lx_topLine, OBJC_ASSOCIATION_RETAIN);
}

- (UIImageView *)lx_bottomLine
{
    UIImageView *line = objc_getAssociatedObject(self, &kLXBottomLineKEY);
    if (!line) {
        line = [[UIImageView alloc] init];
        [self setLx_bottomLine:line];
        [self.contentView addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        line.translatesAutoresizingMaskIntoConstraints = NO;
        NSDictionary *views = NSDictionaryOfVariableBindings(line);
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[line]|" options:0 metrics:nil views:views]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[line(0.5)]|" options:0 metrics:nil views:views]];
    }
    return line;
}

- (void)setLx_bottomLine:(UIImageView *)lx_bottomLine
{
    objc_setAssociatedObject(self, &kLXBottomLineKEY, lx_bottomLine, OBJC_ASSOCIATION_RETAIN);
}

@end
